/*
 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.example;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Timeout;
import org.openjdk.jmh.annotations.Warmup;

/**
 * @author Aaron
 */
public class MyBenchmark {

  @Benchmark
  @Threads(2)
  @Fork(2)
  @BenchmarkMode(Mode.All)
  @Warmup(iterations = 1, time = 3)
  @Measurement(iterations = 10, time = 1)
  @Timeout(time = 300)
  public void testMethod() {
    // 根据路由总数设置随机路由path
    int path = 9997;
    testRoute(path);
  }

  /**
   * 待测试方法
   */
  public void testRoute(int path) {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
        .url("http://127.0.0.1:8888/mock/" + path)
        .build();
    Call call = client.newCall(request);
    try {
      Response response = call.execute();
      assert response.body() != null;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}
